﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBUtil;
using ZXCompany.Models.Entity;
using System.Data.SqlClient;
using System.Data;
namespace ZXCompany.Models.Dao
{
    public class ZX_SEODao
    {
        private Database db = new Database();
        string sqlstr = "";

        /// <summary>
        /// 数据插入
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Insert(ZX_SEO cinfo, SqlConnection sconn,SqlTransaction stran)
        {
            sqlstr = @"INSERT INTO [ZX_SEO]
                           ([keywords]
                            ,[description]
                            ,[author])
                     VALUES
                           (@keywords
                            ,@description
                            ,@author)";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@keywords",Database.SqlNull(cinfo.keywords)),
                new SqlParameter("@description",Database.SqlNull(cinfo.description)),
                new SqlParameter("@author",Database.SqlNull(cinfo.author))
            };
            try
            {
                db.Udaquery(sqlstr, sconn, stran, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Delete(ZX_SEO cinfo, SqlConnection sconn, SqlTransaction stran)
        {
            sqlstr = @"delete [ZX_SEO]";
            try
            {
                db.Udaquery(sqlstr, sconn, stran);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 数据选择
        /// </summary>
        /// <param name="sconn"></param>
        /// <returns></returns>
        public ZX_SEO Select(SqlConnection sconn)
        {
            ZX_SEO Cinfo = new ZX_SEO();
            sqlstr = @"select top 1 isnull(id,0) as id
                        ,isnull([keywords],'') as keywords
                        ,isnull([description],'') as description
                        ,isnull([author],'') as author
                    from ZX_SEO";
            try
            {
                DataTable dt = db.Squery(sqlstr, sconn);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    //Cinfo.ID = Convert.ToInt32(dr["id"]);
                    Cinfo.keywords = dr["keywords"].ToString();
                    Cinfo.description = dr["description"].ToString();
                    Cinfo.author = dr["author"].ToString();
                }
                else
                {
                    Cinfo.ID = 0;
                    Cinfo.keywords = "";
                    Cinfo.description = "";
                    Cinfo.author = "";
                }
                return Cinfo;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}